package com.itheima.controller;

import com.itheima.pojo.Result;
import com.itheima.util.AliyunOSSProperties;
import com.itheima.util.AliyunOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

//这里是专门处理文件上传的Controller
@RestController
@Slf4j
public class UploadController {
//    @Value("https://")//直接赋值

    @Autowired
    private AliyunOSSProperties aliyunOSSProperties;

    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {

        log.info("文件上传开始,文件名:{}",file.getOriginalFilename());
       // 将file  比如 9.jpg
        //获取字节数组
        byte[] content = file.getBytes();
        //获取文件后缀名
        String originalFilename = file.getOriginalFilename();
        String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
        //调用工具类的方法
        String url = AliyunOSSUtils.upload(aliyunOSSProperties.getEndpoint(), aliyunOSSProperties.getBucketName(), content, extName);


        return Result.success(url);
    }
}
